<template>
  <div class="relative">
    <page-loading></page-loading>
    <template v-for="(el, idx) in fields" v-if="idx!=0">
      <os-switch :item="el" v-model='netdogData[idx]' :key="idx" v-if="el.type=='switch'"></os-switch>
      <os-input :item="el" v-model='netdogData[idx]' :key="idx" v-if="el.type=='input'"></os-input>
      <os-select :item="el" v-model='netdogData[idx]' :options="options" 
      :key="idx" v-if="el.type=='select'" :wholeField="true">
      </os-select>
    </template>
    <os-entry>
      <div class="btn btn-blue" @click="set_netwatchdog">{{$t("btn.save")}}</div>
    </os-entry>
  </div>
</template>

<script>
export default {
  data(){
    return {
      fields: this.$t('advanced.pages[5].fields'),
      options: this.$t('advanced.pages[5].netdogExeOptions'),
      netdogData: ['', '', '', '', "", ],
    }
  },
  methods: {
    get_netwatchdog() {
      this.$req.get('/webapi?op=get_netwatchdog').then(res => {
        console.log(res)
        this.netdogData = this.obj_to_arr(res.data, ["mobile_priority", "detect_host1",
         "detect_host2", "detect_itv", "action"]) 
        // obj_to_arr
      })
    },
    set_netwatchdog() {
      if(!this.isLegalHost(...this.filterValue(this.netdogData[1], this.netdogData[2]))){
        // console.log("error")
        return
      }
      // console.log("ok")
      // return 
      let params = this.arr_to_obj(this.netdogData, ["mobile_priority", "detect_host1",
        "detect_host2", "detect_itv", "action"])
      delete params.mobile_priority
      console.log(params)
      this.proxy_req("get", "/webapi?op=set_netwatchdog", params, ()=> {

      }, true)
    },
  },
  created(){
    console.log(this.fields)
    this.$bus.emit('changeTab', 4)
    this.get_netwatchdog()
  },
  deactivated() {

  },
  activated() {
    // 只有当前级别的router-view缓存才会
    // 同步二级路由
    this.$bus.emit('changeTab', 4)
  },
}
</script>

<style lang="scss" scoped>

</style>
